package com.china08.yxyapi.api.manage.tec.teaching.lecture.chooseprepare;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.lecture.YxyLecturePrePackRepository;

/**
 * 
 * 挑选备课包--平台备课包--挑选完成
 *
 */
@CrossOrigin
@RestController
public class LectureCrtChoosePackController extends YxyApiBaseController {

    @Autowired
    private YxyLecturePrePackRepository yxyLecturePrePackRepository;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/lecture/chooseprepare/crt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        String userId = getProfile(request, response).getId();
        String userNick = getProfile(request, response).getUserNick();
        String schoolId = getProfile(request, response).getSchoolId();
        String schoolNick = getProfile(request, response).getSchoolName();
        String areaIdCity = getProfile(request, response).getSchoolAreaIdCity();
        String district = getProfile(request, response).getSchoolAreaIdDistrict();
        String province = getProfile(request, response).getSchoolAreaIdProvince();
        List<String> idsList = reqModel.ids;
        for (String packId : idsList) {
            YxyLecturePrePack pack = yxyLecturePrePackRepository.findById(packId);
            pack.setUsedCount(pack.getUsedCount() + 1l);
            YxyLecturePrePack lecturePrePack = new YxyLecturePrePack();
            lecturePrePack.setAreaIdCity(areaIdCity);
            lecturePrePack.setAreaIdDistrict(district);
            lecturePrePack.setAreaIdProvince(province);
            lecturePrePack.setBookcontentsId(pack.getBookcontentsId());
            lecturePrePack.setBookcontentsName(pack.getBookcontentsName());
            lecturePrePack.setCreatedBy(pack.getCreatedBy());
            lecturePrePack.setIsYx(0);
            lecturePrePack.setPackName(pack.getPackName());
            lecturePrePack.setPhaseId(pack.getPhaseId());
            lecturePrePack.setPhaseName(pack.getPhaseName());
            lecturePrePack.setPv(0l);
            lecturePrePack.setResources(pack.getResources());
            lecturePrePack.setSchoolId(schoolId);
            lecturePrePack.setSchoolNick(schoolNick);
            lecturePrePack.setSemesterId(pack.getSemesterId());
            lecturePrePack.setSemesterName(pack.getSemesterName());
            lecturePrePack.setShare(0);
            lecturePrePack.setStatus(1);
            lecturePrePack.setSubjectId(pack.getSubjectId());
            lecturePrePack.setSubjectName(pack.getSubjectName());
            lecturePrePack.setUsedCount(0l);
            lecturePrePack.setUserId(userId);
            lecturePrePack.setUserNick(userNick);
            lecturePrePack.setVersionId(pack.getVersionId());
            lecturePrePack.setVersionName(pack.getVersionName());
            lecturePrePack.setIsDelete(0);
            lecturePrePack.setReferencId(pack.getId());
            yxyLecturePrePackRepository.save(lecturePrePack);
            yxyLecturePrePackRepository.save(pack);
        }
        RespModel model = new RespModel();
        model.setMessage("挑选完成");
        return model;
    }

    protected static class ReqModel extends YxyApiPageReqModel {

        public List<String> ids;

        public String bookContentsId;

    }

    protected static class RespModel extends YxyApiRespModel {

    }

}
